package cate.game.activity.specialtraining.po;

import cate.common.table.activity.specialtraining.shop.SpecialTrainingShopRow;
import cate.common.table.activity.specialtraining.shop.SpecialTrainingShopSheet;
import cate.common.table.d.GDObj;
import cate.common.util.GameResult;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.specialtraining.SpecialTrainingActivity;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResItem;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@NoteClass(value = "特训活动-商品")
public class SpecialTrainingActivityShopPO extends SubActivitySingle<SpecialTrainingActivity> {
    @NoteField(value = "商品ID对应购买次数")
    public Map<Integer, Integer> shopItemBuyNumMap;

    @Override
    public void initialize(SpecialTrainingActivity parent) {
        super.initialize(parent);
        if (shopItemBuyNumMap == null) {
            shopItemBuyNumMap = new HashMap<>();
        }
    }

    public void excelCheck() {
        SpecialTrainingShopSheet configSheet = parent.getShopSheet();
        if (configSheet == null) {
            shopItemBuyNumMap.clear();
            return;
        }
        shopItemBuyNumMap.entrySet().removeIf(entry ->  configSheet.get(entry.getKey()) == null);
        for (SpecialTrainingShopRow configRow : configSheet.getList()) {
            if (!shopItemBuyNumMap.containsKey(configRow.id)) {
                shopItemBuyNumMap.put(configRow.id, 0);
            }
        }
    }

    public void beforeOver() {

    }

    public void clear() {
        shopItemBuyNumMap.clear();
    }

    /**
     * 获取商品配置
     */
    @JsonIgnore
    public SpecialTrainingShopRow getShopItemConfig(int itemTid) {
        SpecialTrainingShopSheet configSheet = parent.getShopSheet();
        if (configSheet == null) {
            return null;
        }
        return configSheet.get(itemTid);
    }

    /**
     * 购买特训商品
     *
     * @param itemTid 商品ID
     * @return 购买结果 无携带数据
     */
    public EcResult<MixResItem> buyShopItem(int itemTid) {
        EcResult<MixResItem> r = new EcResult<>();
        SpecialTrainingShopRow config = getShopItemConfig(itemTid);
        if (config == null) {
            return r.fail("找不到商品");
        }
        if (!shopItemBuyNumMap.containsKey(itemTid)) {
            return r.fail("找不到商品");
        }
        if (config.buyType != SpecialTrainingShopSheet.ST_BUY_TYPE_ITEMS
                && config.buyType != SpecialTrainingShopSheet.ST_BUY_TYPE_FREE) {
            return r.fail("不支持购买");
        }
        if (config.day > parent.day) {
            return r.fail("商品购买未开启");
        }
        if (config.buyLimit <= shopItemBuyNumMap.get(itemTid)) {
            return r.fail("商品已购完");
        }
        if (role.getBase().vip < config.needVip) {
            return r.fail("VIP等级不足");
        }
        if (config.buyType == SpecialTrainingShopSheet.ST_BUY_TYPE_ITEMS) {
            MixRes costRes = new MixRes(config.costStr);
            r = costRes.consumeCheck(role, null);
            if (!r.ok()) {
                return r.fail(r.message);
            }
            role.log.setOperation(parent.getGDOperation(), itemTid);
            costRes.consume(role);
            role.log.clearOperation();
        }
        provideReward(itemTid);
        return r.success();
    }

    /**
     * 是否支持支付购买商品
     *
     * @param itemTid 商品ID
     * @return 检测结果 无携带数据
     */
    public GameResult<Void> allowBuy(int itemTid) {
        GameResult<Void> r = new GameResult<>();
        SpecialTrainingShopRow config = getShopItemConfig(itemTid);
        if (config == null) {
            return r.fail("找不到商品");
        }
        if (!shopItemBuyNumMap.containsKey(itemTid)) {
            return r.fail("找不到商品");
        }
        if (config.buyType != SpecialTrainingShopSheet.ST_BUY_TYPE_PAY) {
            return r.fail("不支持购买");
        }
        if (config.day > parent.day) {
            return r.fail("商品购买未开启");
        }
        if (config.buyLimit <= shopItemBuyNumMap.get(itemTid)) {
            return r.fail("商品已购完");
        }
        if (role.getBase().vip < config.needVip) {
            return r.fail("VIP等级不足");
        }
        return r.success();
    }

    /**
     * 发放奖励
     *
     * @param itemTid 商品ID
     */
    public void provideReward(int itemTid) {
        SpecialTrainingShopRow config = getShopItemConfig(itemTid);
        if (config == null) {
            return;
        }
        if (!shopItemBuyNumMap.containsKey(itemTid)) {
            return;
        }
        MixRes rewardRes = new MixRes(config.rewardStr);
        if (rewardRes.isEmpty()) {
            return;
        }
        shopItemBuyNumMap.put(itemTid, shopItemBuyNumMap.get(itemTid) + 1);
        MixRes showRes = rewardRes.copy();
        Iterator<MixResItem> iter = rewardRes.items.iterator();
        while (iter.hasNext()) {
            MixResItem item = iter.next();
            if (item.type == GDObj.Type.NUM && item.tid == parent.getPointItemTid()) {
                parent.pointPO.point += item.num;
                iter.remove();
            }
        }
        new MixResAdder().setRes(rewardRes)
                .setOperation(parent.getGDOperation())
                .setOperationSub(itemTid)
                .setSendMailWhenFull(true)
                .setDialogReward(false)
                .exe(role);
        role.getGame().notice.dialogReward(role, showRes);
    }
}
